using MicroMsg.Common.Timer;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
namespace MicroMsg.Scene.Image
{
	public class DownloadImgService
	{
		private const string TAG = "DownloadImgService";
		private const int MAX_RUNNING = 5;
		private static TimerObject mTimerObject;
		public static int startDoSence(int imgMsgSvrId, NetSceneDownloadImg instance, string toTalker, int compressType = 0)
		{
			if (DownloadImgContextMgr.getInstance().countRunningContext() >= 5)
			{
				return 0;
			}
			if (imgMsgSvrId <= 0)
			{
				Log.smethod_1("DownloadImgService", "DownLoad image fail because of imgMsgSvrId <= 0");
				return 0;
			}
			MsgTrans msgTrans = StorageMgr.msgImg.getByMsgSvrID(imgMsgSvrId);
			if (msgTrans == null)
			{
				if (compressType == 0)
				{
					Log.smethod_1("DownloadImgService", "Can not find the Image information DownLoad image failed! ");
					return 0;
				}
				if (compressType == 1)
				{
					ChatMsg bySvrID = StorageMgr.chatMsg.getBySvrID(toTalker, imgMsgSvrId);
					if (bySvrID == null)
					{
						Log.smethod_1("DownloadImgService", "Can not find the Image information msg DownLoad Origin image failed! ");
						return 0;
					}
					msgTrans = new MsgTrans();
					msgTrans.nMsgSvrID = imgMsgSvrId;
					msgTrans.strThumbnail = bySvrID.strThumbnail;
					msgTrans.strFromUserName = toTalker;
					msgTrans.strToUserName = AccountMgr.getCurAccount().strUsrName;
					msgTrans.nTransType = 2;
					StorageMgr.msgImg.method_1(msgTrans);
				}
			}
			DownloadImgContext contextBySvrid = DownloadImgContextMgr.getInstance().getContextBySvrid(imgMsgSvrId);
			if (contextBySvrid != null)
			{
				if (contextBySvrid.senceHandle.mCompressType != 0 || instance.mCompressType != 1)
				{
					Log.smethod_1("DownloadImgService", "imgMsgSvrId: " + imgMsgSvrId + "has been in Queue");
					return -1;
				}
				DownloadImgContextMgr.getInstance().remove(contextBySvrid);
			}
			DownloadImgContext downloadImgContext = new DownloadImgContext();
			downloadImgContext.mImgInfo = msgTrans;
			downloadImgContext.fileStream = downloadImgContext.getImgFileStream();
			if (downloadImgContext.fileStream == null)
			{
				return 0;
			}
			downloadImgContext.senceHandle = instance;
			downloadImgContext.mStatus = 0;
			DownloadImgContextMgr.getInstance().putToHead(downloadImgContext);
			DownloadImgService.checkReadyContextDispatcher();
			return 1;
		}
		private static void checkReadyContextDispatcher()
		{
			if (DownloadImgService.mTimerObject == null)
			{
				DownloadImgService.mTimerObject = TimerService.addTimer(8, new EventHandler(DownloadImgService.onImageDownContextDispatcher), 1, -1);
				DownloadImgService.mTimerObject.start();
			}
		}
		public static void onImageDownContextDispatcher(object sender, EventArgs e)
		{
			DownloadImgContextMgr.getInstance().clearnFinishedContext();
			if (DownloadImgContextMgr.getInstance().countRunningContext() >= 5)
			{
				return;
			}
			DownloadImgContext firstContextNeedHandle = DownloadImgContextMgr.getInstance().getFirstContextNeedHandle();
			if (firstContextNeedHandle == null)
			{
				if (DownloadImgContextMgr.getInstance().countRunningContext() == 0)
				{
					DownloadImgService.mTimerObject.stop();
					DownloadImgService.mTimerObject = null;
				}
				return;
			}
			if (firstContextNeedHandle.mImgInfo != null && firstContextNeedHandle.fileStream != null && firstContextNeedHandle.mImgInfo.strFromUserName != null)
			{
				if (firstContextNeedHandle.senceHandle == null)
				{
					firstContextNeedHandle.senceHandle = new NetSceneDownloadImg();
				}
				firstContextNeedHandle.senceHandle.mOnDownSceneFinished += new onSceneDownFinishedDelegate(DownloadImgService.onSceneFinished);
				firstContextNeedHandle.senceHandle.doScene(firstContextNeedHandle);
				return;
			}
			Log.smethod_1("DownloadImgService", "fillContext fail!");
			firstContextNeedHandle.mStatus = 5;
		}
		private static void onSceneFinished(DownloadImgContext context)
		{
		}
	}
}
